package com.drinks.yiniang.ui.activity.shop;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.shop.ShopJson;
import com.drinks.yiniang.utils.FileUtils;
import com.drinks.yiniang.utils.MapNavUtils;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.dialog.ActionSheetDialog;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.ClipboardUtils;
import com.hansen.library.utils.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;

/**
 * 门店地图详情
 *
 * @author han
 * @date 2019-11-01
 */
public class ShopMapActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    //nav
    private NavigationBarLayout nav_bar_shop_map;
    //地图
    private MapView view_shop_map;
    //地址
    private TextView tv_shop_map_address;
    //电话
    private TextView tv_shop_map_mobile;
    //导航
    private Button btn_shop_map_navigation;
    //分享
    private Button btn_shop_map_share;

    private MarkerOptions markerOption;
    private AMap aMap;

    private ShopJson.ShopList mShopData;

    private LatLng latlng = new LatLng(0,0);

    @Override
    protected void initView() {
        nav_bar_shop_map = findViewById(R.id.nav_bar_shop_map);

        view_shop_map = findViewById(R.id.view_shop_map);

        tv_shop_map_address = findViewById(R.id.tv_shop_map_address);
        tv_shop_map_mobile = findViewById(R.id.tv_shop_map_mobile);

        btn_shop_map_navigation = findViewById(R.id.btn_shop_map_navigation);
        btn_shop_map_share = findViewById(R.id.btn_shop_map_share);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        view_shop_map.onCreate(savedInstanceState); // 此方法必须重写

        mShopData = getIntent() != null ? (ShopJson.ShopList) getIntent().getSerializableExtra(Constants.KeyObject) : null;

        setShopData();
    }

    @Override
    protected void initListener() {
        nav_bar_shop_map.setOnNavgationBarClickListener(this);
        btn_shop_map_navigation.setOnClickListener(this);
        btn_shop_map_share.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_shop_map_navigation://导航
                doNavMap();
                break;
            case R.id.btn_shop_map_share://分享

                break;
            default:
                break;
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_shop_map;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {
        final String telphone = mShopData != null ? mShopData.getTelephone() : null;

        if(StringUtils.isEmpty(telphone)) {
            return;
        }

        new ActionSheetDialog(mContext)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem(telphone, ActionSheetDialog.SheetItemColor.Black,
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                Intent intent = new Intent(Intent.ACTION_DIAL);
                                Uri data = Uri.parse("tel:" + telphone);
                                intent.setData(data);
                                startActivity(intent);
                            }
                        }).show();
    }

    //地图上添加marker
    private void addMarkersToMap() {
        // 设置当前地图显示为当前位置
        aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 19));

        markerOption = new MarkerOptions().icon(BitmapDescriptorFactory
                .defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
                .position(latlng)
                .draggable(true);

        aMap.addMarker(markerOption);
    }

    private void doNavMap() {
        if(mShopData == null) {
            return;
        }

        ActionSheetDialog sheetDialog = new ActionSheetDialog(mContext)
                .builder()
                .setCancelable(true)
                .setCanceledOnTouchOutside(true)
                .addSheetItem(getString(R.string.text_copy_shop_address),
                        new ActionSheetDialog.OnSheetItemClickListener() {
                            @Override
                            public void onClick(int which) {
                                ClipboardUtils.copyTextToClipboard(mContext, StringUtils.getNoNullString(mShopData.getAddress()));
                            }
                        });

        if(FileUtils.isAppInstalled(FileUtils.PACKAGE_NAME_GAODE_MAP)) {
            sheetDialog.addSheetItem(getString(R.string.text_gaode_map),
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            MapNavUtils.goToGaoDeMap(mContext, mShopData.getAddress(), latlng.latitude, latlng.longitude);
                        }
                    });
        }

        if(FileUtils.isAppInstalled(FileUtils.PACKAGE_NAME_BAIDU_MAP)) {
            sheetDialog.addSheetItem(getString(R.string.text_baidu_map),
                    new ActionSheetDialog.OnSheetItemClickListener() {
                        @Override
                        public void onClick(int which) {
                            MapNavUtils.goToBaiduMap(mContext, mShopData.getAddress(), latlng.latitude, latlng.longitude);
                        }
                    });
        }

        sheetDialog.show();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        view_shop_map.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        view_shop_map.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        view_shop_map.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        view_shop_map.onDestroy();
    }

    private void setShopData() {
        if(mShopData == null) {
            return;
        }

        if(!StringUtils.isEmpty(mShopData.getLng_lat())) {
            String[] array = mShopData.getLng_lat().split(",");

            if(array.length >= 2 && StringUtils.isNumber(array[0]) && StringUtils.isNumber(array[1])) {

                DecimalFormat format = new DecimalFormat("0.000000", new DecimalFormatSymbols(Locale.US));
                latlng = new LatLng(Double.parseDouble(format.format(new BigDecimal(array[1]))),Double.parseDouble(format.format(new BigDecimal(array[0]))));
            }
        }

        if(aMap == null) {
            aMap = view_shop_map.getMap();
        }

        addMarkersToMap();// 往地图上添加marker

        //地址
        StringBuilder builder = new StringBuilder(getString(R.string.text_address_colon));
        builder.append(StringUtils.getNullEmptyConvert__(mShopData.getAddress()));
        tv_shop_map_address.setText(builder);
        //电话
        builder = new StringBuilder(getString(R.string.text_tel_colon));
        builder.append(StringUtils.getNullEmptyConvert__(mShopData.getTelephone()));
        tv_shop_map_mobile.setText(builder);
    }
}
